﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Onixs.CmeFastHandler;

namespace TAS
{
    public class SecurityEntity
    {
        public String ID { get; set; }
        public String Desc { get; set; }

        public SecurityStatus securityStatus;

        public IOrderBook regularOrderBook;
        public IOrderBook impliedOrderBook;
        public IOrderBook consolidatedOrderBook;

        public IPriceLevel bestBid; // regular OB
        public IPriceLevel bestAsk; // regular OB
        public IPriceLevel bestBidIOB;
        public IPriceLevel bestAskIOB;
        public IPriceLevel bestBidCOB;
        public IPriceLevel bestAskCOB;

        public long lastTradedPrice;

        public double netPosition { get; set; }
        public long netValue { get; set; }
                       
        SecurityTradeDataEntity[] tradeDatainTimeSpan = new SecurityTradeDataEntity[9];
        int[] timeperiods = { 1, 2, 5, 10, 30, 60, 120, 300, 600 };

        public object rwLock = new object();

        public void Initialize(string securityDesc, string securityID)
        {
            this.ID = securityID;
            this.Desc = securityDesc;

            for (int index = 0; index < 9; index++)
            {
                tradeDatainTimeSpan[index] = new SecurityTradeDataEntity();
                tradeDatainTimeSpan[index].SecurityID = securityID;
                tradeDatainTimeSpan[index].Initialize(timeperiods[index]);
            }
        }

        public void UpdateData(TradeDataEntity tradeDataEntity)
        {
            lock (rwLock)
            {
                this.lastTradedPrice = tradeDataEntity.Price;

                foreach (var item in tradeDatainTimeSpan)
                {
                    item.UpdateData(tradeDataEntity);
                }
            }
        }

        public void LogData()
        {
            foreach (var item in tradeDatainTimeSpan)
            {
                item.LogData();
            }
        }

        public OHLCEntity getOHLC(int timespan)
        {
            for (int index = 0; index < 9; index++)
            {
                if (timeperiods[index] == timespan)
                {
                    return tradeDatainTimeSpan[index].getOHLC();
                }
            }

            return null;
        }


        public BollingerEntity getBOLL(int timespan)
        {
            for (int index = 0; index < 9; index++)
            {
                if (timeperiods[index] == timespan)
                {
                    return tradeDatainTimeSpan[index].getBOLL();
                }
            }

            return null;
        }
    }
}
